package erwiki.ui.app.admin.handlers;

import java.security.Permission;

import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.swt.widgets.Shell;
import org.elwiki.permissions.WikiPermission;

import erwiki.api.auth.AuthorizationManager;
import erwiki.api.core.Engine;
import erwiki.api.core.WikiSession;
import erwiki.api.rwc.RWC;
import jakarta.inject.Inject;

public class Test2Handler {

	@Inject
	Engine engine;

	@Inject
	public void addingContext(IEclipseContext context) {
		// create a new IEclipseContext instance
		IEclipseContext myContext = EclipseContextFactory.create();

		// add objects to context
		String ID = "org.elwiki.e4.ui.app.admin.definition1";
//	    myContext.set(ID, Boolean.TRUE);
//	    context.declareModifiable(ID);
		myContext.set("myactivePartId", "com.example.e4.rcp.todo.part.todooverview");
	}

	/*
	@CanExecute
	boolean isVisible(@Named(IServiceConstants.ACTIVE_PART) MPart part) {
	//		boolean visible = null != part && part.getObject() instanceof MyPart;
	//		refreshToolItem.setVisible(visible);
		return true;
	}*/

	@Execute
	public void execute(Shell shell) {
		// MessageDialog.openInformation(shell, "Test2", "Test2 -- Eclipse 4 RCP Application");
		AuthorizationManager mgr = engine.getManager(AuthorizationManager.class);

		// Verify user is allowed to save profile!
		String appName = engine.getApplicationName();
		Permission p = new WikiPermission(appName, WikiPermission.EDIT_PROFILE_ACTION);

		WikiSession session = RWC.INSTANCE.getWikiSession();
		if (!mgr.checkPermission(session, p)) {
			// throw new WikiSecurityException("You are not allowed to save wiki profiles.");
		}
	}

}
